/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 *
 * @author Kossel
 */
public class ConfigurationLoader {
        private Properties properties;
        private String fileName;
        private DbConfiguration dbConfig;

        public ConfigurationLoader(String fileName){
            this.properties = new Properties();
            this.fileName=fileName;
        }

        private void readConfigFile() throws IOException{  
        InputStream is = Thread.currentThread().getContextClassLoader().getResource(fileName).openStream();
        getProperties().load(is);
        }

        private void setConfiguration(){
           this.dbConfig = new DbConfiguration();
           getDbConfig().setHost(properties.getProperty("host"));
           getDbConfig().setDbUser(properties.getProperty("user"));
           getDbConfig().setDbPass(properties.getProperty("password"));
           getDbConfig().setDbPort(properties.getProperty("port", "3306"));
           getDbConfig().setDbSchema(properties.getProperty("Schema"));
           getDbConfig().setDriver(properties.getProperty("driver", "com.mysql.jdbc.Driver"));
        }

        public DbConfiguration loadConfig()throws IOException{
            readConfigFile();
            
            setConfiguration();
            
            return getDbConfig();
        
        }

    /**
     * @return the properties
     */
    private Properties getProperties() {
        return properties;
    }

    /**
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * @return the dbConfig
     */
    private DbConfiguration getDbConfig() {
            return dbConfig;
    }

    /**
     * @param fileName the fileName to set
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }


}
